package com.nt.train.service.context;

import com.nt.train.service.IRequestStrategy;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * 工厂模式处理
 * @author hlf
 * @version 1.0
 * @date 2022/8/5 14:45
 */
@Component
public class HandlerRequestFactory {

    public static Map<Integer, IRequestStrategy> requestStrategyMap = new ConcurrentHashMap<>();

    public static void putStrategy(Integer id,IRequestStrategy strategy){
        requestStrategyMap.put(id, strategy);
    }

    public IRequestStrategy getStrategyByRequest(Integer type){
        IRequestStrategy requestStrategy = requestStrategyMap.get(type);
        if(requestStrategy == null){
            throw new IllegalArgumentException(String.format("没有对应的指令[%d]", type));
        }
        //获取对应的策略
        return requestStrategy;
    }
}
